<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/7
 * Time: 16:00
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\TeamBrokerage;

class BrokerageService {
    /*
     * 佣金初始化
     */
    public static function money_init($userID){
        $result['total'] = CommonFunction::formatMoney(self::money_total($userID));
        $result['list'] = self::money_list($userID);
        return $result;
    }

    /*
     * 总佣金数
     */
    public static function money_total($userID){
        $money = 0;
        $brokerage = TeamBrokerage::find()->where(['userID'=>$userID])->sum('brokerage');
        if($brokerage)
            $money = $brokerage;
        return $money;
    }



    /*
     * 佣金列表
     */
    public static function money_list($userID, $page = 1, $limit = 10){
        if($page <= 0)
            $page = 1;
        if($limit <= 0)
            $limit = 10;
        $start = (intval($page) - 1) * $limit;
        $list = TeamBrokerage::find()->where(['userID'=>$userID])->with('user')->asArray()->limit($limit)->offset($start)->orderBy('id desc')->all();
        $result = [];
        foreach($list as $v){
            $data['brokerage'] = CommonFunction::formatMoney($v['brokerage']);
            $data['time'] = date('Y-m-d H:i:s', $v['createTime']);
            $data['user'] = CommonFunction::dealTel(CommonFunction::decrypt($v['user']['phone']));
            $result[] = $data;
        }
        return $result;
    }
	
	/*
     * 条目总数
     */
    public static function money_list_count($userID){
        return TeamBrokerage::find()->where(['userID'=>$userID])->count();
    }
}